package com.hz.friends.serverrequest;

import java.util.HashMap;

import org.json.JSONException;

import android.R.string;

import com.alibaba.fastjson.JSONObject;
import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVInstallation;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVPush;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.RefreshCallback;
import com.hz.friends.serverrequest.ALCCPEngine.ALMessageOfChatType;
import com.hz.friends.serverrequest.ALEngineConfig.CallBack;

public class ALPushCenter {

	public enum ALPushType {

		ALPushTypeOfUndefine("未知", 0), 
		ALPushTypeOfMessageOfChat("新的消息", 1), 
		ALPushTypeOfNewFollow("新的关注", 2), 
		ALPushTypeOfGift("美食", 3), 
		ALPushTypeOfMessageOfBBs("娱乐", 4), 
		ALPushTypeOfSpecialRemind("闲聊", 5);

		private int _value;
		private String _name;

		private ALPushType(String name, int value) {
			_name = name;
			_value = value;
		}

		public String get_name() {
			return _name;
		}

		public int get_value() {
			return _value;
		}

		public static ALPushType get_type(int i) {
			ALPushType type = null;
			switch (i) {
			case 0:
				type = ALPushTypeOfUndefine;
				break;
			case 1:
				type = ALPushTypeOfMessageOfChat;
				break;
			case 2:
				type = ALPushTypeOfNewFollow;
				break;
			case 3:
				type = ALPushTypeOfGift;
				break;
			case 4:
				type = ALPushTypeOfMessageOfBBs;
				break;
			case 5:
				type = ALPushTypeOfSpecialRemind;
				break;
			default:
				break;
			}
			return type;
		}
	}
	
	public static void sendPush(
			AVQuery<AVInstallation> installQ,
			ALPushType type,
			int subType){
		
		String alertString = ALPushCenter.getAlertString(type,subType);
		if (alertString == null)
		{
			return;
		}
		
		org.json.JSONObject pushData = new org.json.JSONObject();
		try {
			pushData.put("sound", "message.wav");
			pushData.put("badge", "Increment");
			pushData.put("alert", alertString);
			pushData.put("type", type);
			pushData.put("device","android");
			
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		AVPush.sendDataInBackground(pushData, installQ, null);
	}
	
	public static String getAlertString(
			ALPushType type,
			int subType) {

		String nickname = AVUser.getCurrentUser().getString("nickname");
		String alertStr = null;

		switch (type) {
			case ALPushTypeOfNewFollow:
	        {
	            if (nickname != null)
	            {
	                alertStr = nickname + " : " + "[关注]";
	            }
	            else
	            {
	                alertStr = "你有一个新的粉丝";
	            }
	        }
	            break;
	        case ALPushTypeOfGift:
	        {
	            if (nickname != null)
	            {
	                alertStr = nickname + " : " + "[礼物]";
	            }
	            else
	            {
	                alertStr = "你收到一份礼物";
	            }
	        }
	            break;
	        case ALPushTypeOfMessageOfBBs:
	        {
	            if (nickname != null)
	            {
	                alertStr = nickname + " : " + "[留言]";
	            }
	            else
	            {
	                alertStr = "你收到一个新的留言";
	            }
	        }
	            break;
	        case ALPushTypeOfSpecialRemind:
	        {
	            if (nickname != null)
	            {
	                alertStr = nickname + " : " + "[上线]";
	            }
	            else
	            {
	                alertStr = "你的特别关注上线了";
	            }
	        }
	            break;
	        case ALPushTypeOfMessageOfChat:
	        {
	        	switch (ALMessageOfChatType.get_type(subType)) {
		    		case ALMessageOfChatTypeOfText:
		    		{
		    			if (nickname != null) 
		    			{
		    				alertStr = nickname + " : " + "[文字]";
		    			} 
		    			else 
		    			{
		    				alertStr = "你收到一条消息";
		    			}
		    		}
		    			break;
		    		case ALMessageOfChatTypeOfVoice:
		    		{
		    			if (nickname != null) 
		    			{
		    				alertStr = nickname + " : " + "[语音]";
		    			}
		    			else 
		    			{
		    				alertStr = "你收到一段[语音]";
		    			}
		    		}
		    			break;
		    		case ALMessageOfChatTypeOfImage: 
		    		{
		    			if (nickname != null) 
		    			{
		    				alertStr = nickname + " : " + "[图片]";
		    			}
		    			else
		    			{
		    				alertStr = "你收到一个[图片]";
		    			}
		    		}
		    			break;
		    		case ALMessageOfChatTypeOfVideo:
		    		{
		    			if (nickname != null) 
		    			{
		    				alertStr = nickname + " : " + "[视频]";
		    			} 
		    			else 
		    			{
		    				alertStr = "你收到一段[视频]";
		    			}
		    		}
		    			break;
		    		case ALMessageOfChatTypeOfGroupInvite: 
		    		{
		    			if (nickname != null) 
		    			{
		    				alertStr = nickname + " : " + "[群邀请]";
		    			} 
		    			else
		    			{
		    				alertStr = "你收到一个[群邀请]";
		    			}
		    		}
		    			break;
		    		case ALMessageOfChatTypeOfLocation: 
		    		{
		
		    		}
		    			break;
		    		case ALMessageOfChatTypeOfFile: 
		    		{
		
		    		}
		    			break;
		    		case ALMessageOfChatTypeOfCommand: 
		    		{
		
		    		}
		    			break;
		    		default:
		    			break;
	    		}
	        }
			default:
				break;
		}
		
	return alertStr;
	}
	
	public static void openPushAlert(){
		
		AVUser.getCurrentUser().put("closePushAlert", false);
		AVUser.getCurrentUser().saveEventually();
	}

	public static void closePushAlert(){
		
		AVUser.getCurrentUser().put("closePushAlert", true);
		AVUser.getCurrentUser().saveEventually();
	}

	public static void isClosePushAlert(
			AVUser user,
	        final CallBack boolCallback){

		user.refreshInBackground(new RefreshCallback<AVObject>() {
			
			@Override
			public void done(AVObject obj, AVException e) {
				// TODO Auto-generated method stub
				boolCallback.done(obj.get("closePushAlert"),e);
			}
		});
	}
}
